package common.message
{
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import kernel.application.ApplicationDisplay;
	import common.AppLayers;

	/**
	 * 行为公告
	 * @author flashk
	 * 
	 */
	public class PublicNotice extends Sprite
	{
		private static var _instance:PublicNotice;
		
		private var _messages:Array;
		private var _shows:Array;
		private var _box:Sprite;
		private var _lineHeight:int = 21;
		private var _width:Number = 600;
		
		public function PublicNotice()
		{
			this.y = 80;
			this.x = int((ApplicationDisplay.stage.stageWidth-_width)/2);
			_messages = [];
			_box = new Sprite();
			_box.scrollRect = new Rectangle(0,0,_width,(_lineHeight+1)*2);
			_box.mouseEnabled = false;
			this.addChild(_box);
			this.graphics.beginFill(0x0,0.2);
			this.graphics.drawRect(0,0,_width,(_lineHeight+1)*2);
			this.mouseEnabled = false;
		}

		public static function get instance():PublicNotice
		{
			return _instance;
		}

		/**
		 *初始化 
		 * 
		 */
		public static function init():void
		{
			_instance = new PublicNotice();
			AppLayers.noticeLayer.addChild(_instance);
		}
		
		/**
		 *添加一条消息 
		 * @param message
		 * 
		 */
		public function add(message:String,color:uint=0xFFFFFF):void
		{
			_messages.push([message,color]);
			check(2);
		}
		public function check(type:int=1):void
		{
			if(_messages.length>0 && _box.numChildren<2)
			{
				var one:PublicOneNotice = new PublicOneNotice(_messages[0],check,_width);
				one.x = int(_width/2);
				if(_box.numChildren==0)
				{
					one.y = _lineHeight*_box.numChildren;
				}else
				{
					if(_box.getChildAt(0).y == 0)
					{
						one.y = _lineHeight*_box.numChildren;
					}else
					{
						one.y = _lineHeight*2;
						TweenMax.to(one,0.3,{y:_lineHeight});
					}
				}
				_box.addChild(one);
				_messages.shift();
				if(_box.numChildren>1)
				{
					TweenMax.to(_box.getChildAt(0),0.3,{y:0});
				}
			}else if( _box.numChildren>0)
			{
				TweenMax.to(_box.getChildAt(0),0.3,{y:0});
			}
		}
		
	}
}